package com.zkj.cloud.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zkj.cloud.order.feignService.PaymentFeignService;
import com.zkj.cloud.order.lb.LoadBalance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: DemoController
 * @Description: 包含resttemplate,负载均衡，openFeign，熔断器的使用
 * @Author: 朱开军
 * @Date: 2021-9-9 17:59
 * @Copyright: © 福建瑞术信息 版权所有
 */
@Slf4j
@RestController
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class DemoController {

    /**
     * 支付接口注册到eureka服务的名称
     */
    private final String PAY_SERVICE="http://PAY-SERVER";

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    LoadBalance loadBalance;
    @Autowired
    private PaymentFeignService paymentFeignService;


    @GetMapping("/demo")
    public Object demo(){
        return restTemplate.getForObject(PAY_SERVICE+"/demo/list", JSONObject.class);
    }


    @GetMapping("/test")
    public Object test(){

        List<ServiceInstance> instances = discoveryClient.getInstances("PAY-SERVER");
        if(instances.size()<1){
            log.info("没有注册的接口");
            return "没有注册的接口";
        }

        ServiceInstance serviceInstance = loadBalance.instance(instances);
        URI uri = serviceInstance.getUri();

        return restTemplate.getForObject(uri+"/demo/list", JSONObject.class);
    }


    @GetMapping("/list")
    public Object open(){
        return paymentFeignService.getList();
    }

    @GetMapping("/openDemo")
    public Object openDemo(){
        return paymentFeignService.demo();
    }

    @GetMapping("/hystrix/demo")
   /* @HystrixCommand(fallbackMethod = "PaymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })*/
    //@HystrixCommand
    public String paymentInfo_TimeOut_demo(){
        String result = paymentFeignService.demo();
        return result;
    }

    @GetMapping("/hystrix/list")
    //@HystrixCommand
    public Object paymentInfo_TimeOut_list(){
        //int age = 10/0;
        JSONObject result = paymentFeignService.getList();
        return result;
    }

    public String PaymentTimeOutFallbackMethod() {
        return "我是消费者80，对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己，o(╥﹏╥)o";
    }

    /**
     * 全局fallback方法
     * @return
     */
    public String payment_Global_FallbackMethod(){
        return "Global异常处理信息，请稍后再试，o(╥﹏╥)o";
    }

}
